package com.ruoyi.cm.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.cm.domain.bo.CmBsContractInfoBo;
import com.ruoyi.cm.domain.vo.CmBsContractInfoVo;
import com.ruoyi.cm.service.ICmBsContractInfoService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckLogin;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.cm.domain.vo.CmBsContractInfoMilestoneVo;
import com.ruoyi.cm.domain.bo.CmBsContractInfoMilestoneBo;
import com.ruoyi.cm.service.ICmBsContractInfoMilestoneService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 合同信息里程碑
 *
 * @author zyb
 * @date 2024-09-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/cm/bsContractInfoMilestone")
public class CmBsContractInfoMilestoneController extends BaseController {

    private final ICmBsContractInfoMilestoneService iCmBsContractInfoMilestoneService;
    private final ICmBsContractInfoService iCmBsContractInfoService;

    /**
     * 查询合同信息里程碑列表
     */
    @SaCheckLogin
    @GetMapping("/list")
    public TableDataInfo<CmBsContractInfoMilestoneVo> list(CmBsContractInfoMilestoneBo bo, PageQuery pageQuery) {
        return iCmBsContractInfoMilestoneService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询合同信息里程碑列表
     */
    @SaCheckLogin
    @GetMapping("/listData")
    public R<List<CmBsContractInfoMilestoneVo>> listData(Long bdId) {
        CmBsContractInfoBo cmBsContractInfoBo = new CmBsContractInfoBo();
        cmBsContractInfoBo.setBdId(bdId);
        List<CmBsContractInfoVo> cmBsContractInfoVos = iCmBsContractInfoService.queryList(cmBsContractInfoBo);
        if(CollectionUtils.isNotEmpty(cmBsContractInfoVos)){
            CmBsContractInfoMilestoneBo bo = new CmBsContractInfoMilestoneBo();
            bo.setContractId(cmBsContractInfoVos.get(0).getContractId());
            List<CmBsContractInfoMilestoneVo> list = iCmBsContractInfoMilestoneService.queryList(bo);
            return R.ok(list);
        }
        return R.ok();
    }

    /**
     * 导出合同信息里程碑列表
     */
    @SaCheckLogin
    @Log(title = "合同信息里程碑", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CmBsContractInfoMilestoneBo bo, HttpServletResponse response) {
        List<CmBsContractInfoMilestoneVo> list = iCmBsContractInfoMilestoneService.queryList(bo);
        ExcelUtil.exportExcel(list, "合同信息里程碑", CmBsContractInfoMilestoneVo.class, response);
    }

    /**
     * 获取合同信息里程碑详细信息
     *
     * @param id 主键
     */
    @SaCheckLogin
    @GetMapping("/{id}")
    public R<CmBsContractInfoMilestoneVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iCmBsContractInfoMilestoneService.queryById(id));
    }

    /**
     * 新增合同信息里程碑
     */
    @SaCheckLogin
    @Log(title = "合同信息里程碑", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CmBsContractInfoMilestoneBo bo) {
        return toAjax(iCmBsContractInfoMilestoneService.insertByBo(bo));
    }

    /**
     * 修改合同信息里程碑
     */
    @SaCheckLogin
    @Log(title = "合同信息里程碑", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CmBsContractInfoMilestoneBo bo) {
        return toAjax(iCmBsContractInfoMilestoneService.updateByBo(bo));
    }

    /**
     * 删除合同信息里程碑
     *
     * @param ids 主键串
     */
    @SaCheckLogin
    @Log(title = "合同信息里程碑", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iCmBsContractInfoMilestoneService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
